package com.edu.autoconfigure;

import com.edu.library.Person;
import com.edu.library.PersonConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author cj
 * @date 2018/11/22
 */

@Configuration
@EnableConfigurationProperties(DemoProperties.class)
@ConditionalOnClass(Person.class)
public class DemoAutoConfiguration {

    @Autowired
    private DemoProperties demoProperties;

    @Bean
    @ConditionalOnMissingBean
    public PersonConfig personConfig(){
        String username = demoProperties.getUsername()==null?"default username":demoProperties.getUsername();
        String statement = demoProperties.getStatement() == null ? "default statement" : demoProperties.getStatement();
        PersonConfig personConfig = new PersonConfig();
        personConfig.setName(username);
        personConfig.setWord(statement);
        return personConfig;
    }

    @Bean
    @ConditionalOnMissingBean
    public Person person(PersonConfig personConfig){
        Person person = new Person(personConfig);
        return  person;
    }
}
